<?php
class classAction extends baseAction {
	function index(){
		$rs = D("ju_class");
		$lists = $rs->where("p_id=0")->order("sort asc")->select();
		//二级分类
		foreach($lists as $k=>$v){
			$lists[$k]['children'] = $rs->where("p_id='".$v['id']."'")->order("sort asc")->select();
		}
		//print_r($lists[0]);
		$this->assign("lists",$lists);
		$this->display();
	}

	public function edit(){
		$rs = D("ju_class");
		
		$item = $rs->where("id=".$_GET['id'])->select();
		//echo $rs->getLastSql();
		
		$this->assign("item",$item[0]);
		
		$this->display();
	}
	public function update(){
		$rs = D("ju_class");
		$r = $rs->save($_POST);
		print_r($_POST);
		//echo $rs->getLastSql();
		//echo $r;
		if(1==$r)
			echo "修改成功！<a href='/admin.php?m=class'>列表</a>";
		else
			echo "修改失败，可能你未做修改！<a href='/admin.php?m=class'>列表</a>";

	}
	
	public function del(){
		
		$i = D("ju_item");
		$delId = implode("," ,$_POST['id']);
		$containItem = $i->where("classId in(".$delId.")")->count();
		if($containItem>0){
			echo "你选择的分类中含有商品，请先删除这些商品！蛋疼！<a href='/admin.php?m=class&a=index'>返回</a>";
		}else{
			$rs = D("ju_class");
			$r = $rs->where("id in(".$delId.")")->delete();
			//echo $rs->getLastSql();
			//echo $r;
			if($r>0){
				echo "删除成功！<a href='/admin.php?m=class&a=index'>返回</a>";
			}else{
				echo "操作失败！<a href='/admin.php?m=class&a=index'>返回</a>";
			}
		
		}
		
	}
	
	
	public function add(){
		if($_POST['dosubmit']!=''){
			$rs = D("ju_class");
			$rs->add($_POST);
			echo "操作成功！<a href='/admin.php?m=class&a=index'>返回</a>";
		}else{
			$this->display();
		}
	}
		
	public function status(){
		$rs = D("ju_class");
		$r = $rs->where("id=".$_GET['id'])->find();
		if($r[$_GET['type']]){
			$rs->where("id=".$_GET['id'])->save(array($_GET['type']=>0));
			//echo $rs->getLastSql();
			echo 0;
		}else{
			$rs->where("id=".$_GET['id'])->save(array($_GET['type']=>1));
			echo 1;
		}
		
		//echo $rs->getLastSql();
		//print_r($r);
		
	}

//end of the class
}

?>